//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Fdm2dBlackScholesOp.h"
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Methods/Finitedifferences/Meshers/FdmMesher.h>
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Methods/Finitedifferences/Operators/FdmLinearOpComposite.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Methods::Finitedifferences::Meshers;
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::CFdm2dBlackScholesOp (Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ p1, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ p2, Double correlation, Double maturity, Microsoft::FSharp::Core::FSharpOption<Boolean>^ localVol, Microsoft::FSharp::Core::FSharpOption<Double>^ illegalLocalVolOverwrite) : CFdmLinearOpComposite(CFdm2dBlackScholesOp::typeid)
{
    CFdmMesher^ _Cmesher;
    CGeneralizedBlackScholesProcess^ _Cp1;
    CGeneralizedBlackScholesProcess^ _Cp2;
    try
    {
#ifdef HANDLE
        _phFdm2dBlackScholesOp = NULL;
#endif
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _Cp1 = safe_cast<CGeneralizedBlackScholesProcess^> (p1);
        _Cp1->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _p1 = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cp1->GetShared ()); 
        _Cp2 = safe_cast<CGeneralizedBlackScholesProcess^> (p2);
        _Cp2->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _p2 = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cp2->GetShared ()); 
        QuantLib::Real _correlation = (QuantLib::Real)ValueHelper::Convert (correlation); //d
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity); //d
        bool _localVol = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (localVol) ? (bool)ValueHelper::Convert (localVol->Value) : false); //4
        QuantLib::Real _illegalLocalVolOverwrite = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (illegalLocalVolOverwrite) ? (QuantLib::Real)ValueHelper::Convert (illegalLocalVolOverwrite->Value) : -Null<QuantLib::Real>()); //4
        _ppFdm2dBlackScholesOp = new boost::shared_ptr<QuantLib::Fdm2dBlackScholesOp> (new QuantLib::Fdm2dBlackScholesOp ( _mesher,  _p1,  _p2,  _correlation,  _maturity,  _localVol,  _illegalLocalVolOverwrite ));
        SetFdmLinearOpComposite (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdm2dBlackScholesOp));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
        if (_Cp1 != nullptr) _Cp1->Unlock();
        if (_Cp2 != nullptr) _Cp2->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::CFdm2dBlackScholesOp (boost::shared_ptr<QuantLib::Fdm2dBlackScholesOp>& childNative, Object^ owner) : CFdmLinearOpComposite(CFdm2dBlackScholesOp::typeid)
{
#ifdef HANDLE
	_phFdm2dBlackScholesOp = NULL;
#endif
	_ppFdm2dBlackScholesOp = &childNative;
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdm2dBlackScholesOp));
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::CFdm2dBlackScholesOp (QuantLib::Fdm2dBlackScholesOp& childNative, Object^ owner) : CFdmLinearOpComposite(CFdm2dBlackScholesOp::typeid)
{
#ifdef HANDLE
	_phFdm2dBlackScholesOp = NULL;
#endif
	_ppFdm2dBlackScholesOp = new boost::shared_ptr<QuantLib::Fdm2dBlackScholesOp> (&childNative);
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdm2dBlackScholesOp));
    _Fdm2dBlackScholesOpOwner = owner;
    _FdmLinearOpCompositeOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::CFdm2dBlackScholesOp (CFdm2dBlackScholesOp^ copy) : CFdmLinearOpComposite(CFdm2dBlackScholesOp::typeid)
{
#ifdef HANDLE
	_phFdm2dBlackScholesOp = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdm2dBlackScholesOp = new boost::shared_ptr<QuantLib::Fdm2dBlackScholesOp> (copy->GetShared());
        _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdm2dBlackScholesOp));
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::CFdm2dBlackScholesOp (PLATFORM::Type^ t) : CFdmLinearOpComposite(CFdm2dBlackScholesOp::typeid)
{
#ifdef HANDLE
	_phFdm2dBlackScholesOp = NULL;
#endif
	if (!t->IsSubclassOf(CFdm2dBlackScholesOp::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::CFdm2dBlackScholesOp (QuantLib::Handle<QuantLib::Fdm2dBlackScholesOp>& childNative, Object^ owner)  : CFdmLinearOpComposite(CFdm2dBlackScholesOp::typeid)
{
	_phFdm2dBlackScholesOp = &childNative;
	_ppFdm2dBlackScholesOp = &static_cast<boost::shared_ptr<QuantLib::Fdm2dBlackScholesOp>>(childNative.currentLink());
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdm2dBlackScholesOp));
    _Fdm2dBlackScholesOpOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::CFdm2dBlackScholesOp (QuantLib::Handle<QuantLib::Fdm2dBlackScholesOp> childNative)  : CFdmLinearOpComposite(CFdm2dBlackScholesOp::typeid)
{
	_phFdm2dBlackScholesOp = &childNative;
	_ppFdm2dBlackScholesOp = &static_cast<boost::shared_ptr<QuantLib::Fdm2dBlackScholesOp>>(childNative.currentLink());
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdm2dBlackScholesOp));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::CFdm2dBlackScholesOp (QuantLib::Fdm2dBlackScholesOp childNative)  : CFdmLinearOpComposite(CFdm2dBlackScholesOp::typeid)
{
#ifdef HANDLE
	_phFdm2dBlackScholesOp = NULL;
#endif
	_ppFdm2dBlackScholesOp = new boost::shared_ptr<QuantLib::Fdm2dBlackScholesOp> (new QuantLib::Fdm2dBlackScholesOp (childNative));
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdm2dBlackScholesOp));
}
#endif

Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::~CFdm2dBlackScholesOp ()
{
    if (_ppFdm2dBlackScholesOp != NULL)
    {
	    delete _ppFdm2dBlackScholesOp;
        _ppFdm2dBlackScholesOp = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::!CFdm2dBlackScholesOp ()
{
    if (_ppFdm2dBlackScholesOp != NULL)
    {
	    delete _ppFdm2dBlackScholesOp;
    }
}
QuantLib::Fdm2dBlackScholesOp& Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::GetReference ()
{
    if (_ppFdm2dBlackScholesOp == NULL) throw REFNEW NativeNullException ();
	return **_ppFdm2dBlackScholesOp;
}
boost::shared_ptr<QuantLib::Fdm2dBlackScholesOp>& Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::GetShared ()
{
    if (_ppFdm2dBlackScholesOp == NULL) throw REFNEW NativeNullException ();
	return *_ppFdm2dBlackScholesOp;
}
QuantLib::Fdm2dBlackScholesOp* Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::GetPointer ()
{
    if (_ppFdm2dBlackScholesOp == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdm2dBlackScholesOp;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Fdm2dBlackScholesOp>& Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::GetHandle ()
{
	if (_phFdm2dBlackScholesOp == NULL)
	{
		_phFdm2dBlackScholesOp = new Handle<QuantLib::Fdm2dBlackScholesOp> (*_ppFdm2dBlackScholesOp);
	}
	return *_phFdm2dBlackScholesOp;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::HasNative () 
{
	return (_ppFdm2dBlackScholesOp != NULL);
}

Cephei::QL::Methods::Finitedifferences::Operators::IFdm2dBlackScholesOp^ Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::SetTime (Double t1, Double t2)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _t1 = (QuantLib::Time)ValueHelper::Convert (t1); //a
        QuantLib::Time _t2 = (QuantLib::Time)ValueHelper::Convert (t2); //a
    	(*_ppFdm2dBlackScholesOp)->setTime ( _t1,  _t2 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFdm2dBlackScholesOp)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Operators::IFdm2dBlackScholesOp^ Cephei::QL::Methods::Finitedifferences::Operators::CFdm2dBlackScholesOp_Factory::Create (Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ p1, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ p2, Double correlation, Double maturity, Microsoft::FSharp::Core::FSharpOption<Boolean>^ localVol, Microsoft::FSharp::Core::FSharpOption<Double>^ illegalLocalVolOverwrite)
{
    return REFNEW CFdm2dBlackScholesOp ( mesher,  p1,  p2,  correlation,  maturity,  localVol,  illegalLocalVolOverwrite);
}
